<?php
namespace app\admin\controller;

use think\Controller;
use app\admin\model\User as Users;
use app\admin\model\AuthGroup;
use app\admin\model\AuthGroupAccess;
use app\admin\model\UserInfo;
use think\Db;

class FrontUser extends Common
{
    private $cModel;   //当前控制器关联模型

    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        $where = [];
        if (input('get.search')){
            $where['username|id'] = ['like', '%'.input('get.search').'%'];
        }
        $order = 'id desc';

        $dataList = Db::name('front_user')->where($where)->order($order)->paginate('', false, page_param());

        $this->assign('dataList', $dataList);
        return $this->fetch();
    }

    public function edit($id)
    {
        if (request()->isPost()){
            $data = input('post.');
            if ($data['actions'] == 'user'){    //修改用户基本信息
                $uData = Db::name('front_user')->where('id',$data['id'])->find();
                if($uData){
                    $updateData = [
                        'points'=> $data['points'],
                        'islock'=> $data['islock'],
                        'logintime'=> $data['logintime'],
                        'loginip'=> $data['loginip'],
                        'regdate'=> $data['regdate'],
                        'job'=> $data['job'],
                        'firm'=> $data['firm'],
                        'qq'=> $data['qq'],
                        'mobile'=> $data['mobile']
                    ];
                    if($data['password']){
                        $updateData['password'] =  md5($data['password']);
                    }
                    $result = Db::name('front_user')->where('id',$data['id'])->update($updateData);
                }

            }
            if ($result){
                return ajaxReturn(lang('action_success'), url('index'));
            }else{
                return ajaxReturn($this->cModel->getError());
            }
        }else{
            if ($id > 0){
                $data = Db::name('front_user')->where('id',$id)->find();
                $this->assign('data', $data);
                return $this->fetch();
            }
        }
    }

    public function delete()
    {
        if (request()->isPost()){
            $id = input('id');
            if (isset($id) && !empty($id)){
                Db::startTrans();
                try{
                    $id_arr = explode(',', $id);   //用户数据
                    $where = [ 'id' => ['in', $id_arr] ];
                    $result = Db::name('front_user')->where($where)->delete();   //删除主表数据
                    // 提交事务
                    if ($result){
                        Db::commit();
                        return ajaxReturn(lang('action_success'), url('index'));
                    }else{
                        return ajaxReturn($this->cModel->getError());
                    }
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                    return ajaxReturn($e->getMessage());
                }
            }
        }
    }
}